package org.krosa.net.http.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharsetUtil {
    static String regEx = "<meta[^>]*?charset=[\"]?([^\"]*)?\"\\s*[/]?>";
    static Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);

    public static String getCharSet(byte bytes[]) {
        return getCharSet(new String(bytes));
    }

    public static String getCharSet(String str) {
        String charSet = "GBK";
        try {
            //guess the content
            Matcher m = p.matcher(str);
            if (m.find()) {
                charSet = m.group(1);
                charSet = charSet.toUpperCase();
            }
        } catch (Exception e) {
        }
        return charSet;
    }
}
